package com.tjbank.cssys.web.controller;

import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOrderLoanOutsourcingService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = "CkOrderLoanOutsourcingController",description = "委外数据管理",basePath = "/out/source")
@RestController
@RequestMapping("/out/source")
public class CkOrderLoanOutsourcingController {

    private static final Logger logger = LoggerFactory.getLogger(CkOrderLoanOutsourcingController.class);

    private static final Byte MY_TYPE=1;//未外呼
    private static final Byte YJ_TYPE=2;//已外呼

    @Autowired
    private CkOrderLoanOutsourcingService ckOrderLoanOutsourcingService;

    @Autowired
    private CkLogService ckLogService;

    @ApiOperation("委外数据列表分页查询")
    @PostMapping("/list")
    public CommonResult selectList(@RequestBody Map<String, Object> params , HttpServletRequest request){

        PageInfo pageInfo = ckOrderLoanOutsourcingService.selectList(params);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams("");
        ckLog.setOperationType("委外数据列表分页查询");

        if(ckOrderLoanOutsourcingService!=null){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(pageInfo,"委外数据列表分页查询！");
        }else{
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return  CommonResult.failed("操作有误");
        }
    }


    @ApiOperation("委外数据 外呼处理")
    @PostMapping("/update/status")
    public CommonResult updateStatus(@RequestParam String ids , HttpServletRequest request){
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setLogName(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setUserId(userCode);
        ckLog.setParams(ids+"");
        ckLog.setOperationType("委外数据 外呼处理");
        ckLogService.addInsert(ckLog);
        if(StringUtils.isNotBlank(ids)){
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            ckOrderLoanOutsourcingService.updateStatus(ids,YJ_TYPE);
        }

        return CommonResult.success(null,"外呼下发成功！");
    }
}
